package com.wuyan.locationdog.ui.device

import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.utils.getTestDataList
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.TestListAdapter
import com.wuyan.locationdog.databinding.VoiceListActivityBinding
import com.wuyan.locationdog.view.LoadingPopup
import com.wuyan.locationdog.viewmodel.MsgViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class VoiceActivity : BaseBindingActivity<VoiceListActivityBinding>() {

    private val mViewModel: MsgViewModel by viewModels()

    private val mAdapter by lazy { TestListAdapter(R.layout.item_voice_list, null) }

    override fun initData() {

        binding.apply {
            setTitleText("听音")


            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无消息", com.arcsoft.commonbase.R.mipmap.empty_msg))

                    setNewInstance(getTestDataList(5))
                    setOnItemClickListener { adapter, view, position ->
                        ActivityUtils.startActivity(VoiceDetailActivity::class.java)
                    }
                }
            }

            seekbar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
                override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {

                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {

                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                    if (seekbar.progress > 50) {
                        seekbar.setProgress(100, true)
                        lifecycleScope.launch {
                            delay(200)
                            seekbar.gone()
                            hint.gone()
                            countDown.visible()

                            LoadingPopup(mActivity).showPopupWindow()
                        }
                    } else {
                        seekbar.setProgress(0, true)
                        seekbar.visible()
                        hint.visible()
                        countDown.gone()
                    }
                }
            })
//            setOnRefreshLoadMore(smartRefreshLayout)
        }


    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["pageSize"] = 10
        map["pageNum"] = currentPage
        mViewModel.msgList(map)

    }


    override fun observe() {
        super.observe()

        mViewModel.msgBean.observe(this) {

        }

        mViewModel.anyBean.observe(this) {
            currentPage = 1
            loadNetData()
        }
    }

    override fun onResume() {
        super.onResume()
        loadNetData()

    }
}